//
//  UIImage+Ex.swift
//  App
//
//  Created by LXF on 2018/9/25.
//  Copyright © 2018年 LXF. All rights reserved.
//

import UIKit
import FastComponent

/// UIImagePNGRepresentation
public extension XXExtension where Base:UIImage{
    
    /// 返回 png data
    func dataOfPng()->Data?{
        return self.base.pngData()
    }
    
    /// 返回 jpeg data
    ///
    /// - Parameter compressionQuality: 压缩系数
    func dataOfJpeg(compressionQuality:CGFloat = 1)->Data?{
        return self.base.jpegData(compressionQuality: compressionQuality)
    }
    
}

//extension UIImage {
//    func imageWithColor(tintColor: UIColor) -> UIImage {
//        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
//        
//        let context = UIGraphicsGetCurrentContext()
//        context!.translateBy(x: 0, y: self.size.height)
//        context!.scaleBy(x: 1.0, y: -1.0);
//        context!.setBlendMode(CGBlendMode.normal)
//        
//        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
//        context?.clip(to: rect, mask: self.cgImage!)
//        tintColor.setFill()
//        context!.fill(rect)
//        
//        let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
//        UIGraphicsEndImageContext()
//        
//        return newImage
//    }
//}

public extension XXExtension where Base:UIImage{

    func image(render color:UIColor)->UIImage?{
        UIGraphicsBeginImageContextWithOptions(self.base.size, false, self.base.scale)
        let context = UIGraphicsGetCurrentContext()
        context?.translateBy(x: 0, y: self.base.size.height)
        context?.scaleBy(x: 1, y: -1)
        context?.setBlendMode(CGBlendMode.normal)
        let rect = CGRect.init(origin: CGPoint.zero, size: base.size)
        context?.clip(to: rect, mask: base.cgImage!)
        color.setFill()
        context?.fill(rect)
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return newImage
    }
}
